/**
 *
 * Copyright 2012 Snda, Inc. All rights reserved.
 * StampActivity.java
 *
 */
package com.magic.babyphotos;

import com.magic.babyphotos.adapter.ImageAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ViewFlipper;

/**
 *@author jinjiashun(jinjiashun@snda.com)
 *@date 2012-2-6
 */
public class StampActivity extends BaseActivity implements OnTouchListener
{
    ViewFlipper flip;
    
    private final static int[] TEXT_IDS = new int[]{
        R.drawable.text_accecories,
        R.drawable.text_hats,
        R.drawable.text_items,
        R.drawable.text_labels,
            };
    
    private final static int[] S_AC_IDS = new int[]{
        R.drawable.s_ac001,
        R.drawable.s_ac002,
        R.drawable.s_ac003,
        R.drawable.s_ac004,
        R.drawable.s_ac005,
        R.drawable.s_ac006,
        R.drawable.s_ac007,
        R.drawable.s_ac008,
        R.drawable.s_ac009,
        R.drawable.s_ac010,
        R.drawable.s_ac011,
        R.drawable.s_ac012,
        R.drawable.s_ac013,
        R.drawable.s_ac014,
        R.drawable.s_ac015,
        R.drawable.s_ac016,
        R.drawable.s_ac017,
        R.drawable.s_ac018,
        R.drawable.s_ac019,
        R.drawable.s_ac020,
        
    };
    
    private final static int[] S_HAT_IDS = new int[]{
        R.drawable.s_hat001,
        R.drawable.s_hat002,
        R.drawable.s_hat003,
        R.drawable.s_hat004,
        R.drawable.s_hat005,
        R.drawable.s_hat006,
        R.drawable.s_hat007,
        R.drawable.s_hat008,
        R.drawable.s_hat009,
        R.drawable.s_hat010,
        R.drawable.s_hat011,
        R.drawable.s_hat012,
        R.drawable.s_hat013,
        R.drawable.s_hat014,
        R.drawable.s_hat015,
        R.drawable.s_hat016,
        R.drawable.s_hat017,
        R.drawable.s_hat018,
        R.drawable.s_hat019,
        R.drawable.s_hat020,
        R.drawable.s_hat021,
        R.drawable.s_hat022,
        R.drawable.s_hat023,
        R.drawable.s_hat024,
        R.drawable.s_hat025,
        R.drawable.s_hat026,
        R.drawable.s_hat027,
        R.drawable.s_hat028,
        R.drawable.s_hat029,
        R.drawable.s_hat030,
        R.drawable.s_hat031,
        R.drawable.s_hat032,
        R.drawable.s_hat033,
        R.drawable.s_hat034,
        R.drawable.s_hat035,
        R.drawable.s_hat036,
    };
    
    private final static int[] S_ITEM_IDS = new int[]{
        R.drawable.s_item028,
        R.drawable.s_item001,
        R.drawable.s_item002,
        R.drawable.s_item003,
        R.drawable.s_item004,
        R.drawable.s_item005,
        R.drawable.s_item006,
        R.drawable.s_item007,
        R.drawable.s_item008,
        R.drawable.s_item009,
        R.drawable.s_item010,
        R.drawable.s_item011,
        R.drawable.s_item012,
        R.drawable.s_item013,
        R.drawable.s_item014,
        R.drawable.s_item015,
        R.drawable.s_item016,
        R.drawable.s_item017,
        R.drawable.s_item018,
        R.drawable.s_item019,
        R.drawable.s_item020,
        R.drawable.s_item021,
        R.drawable.s_item022,
        R.drawable.s_item023,
        R.drawable.s_item024,
        R.drawable.s_item025,
        R.drawable.s_item026,
        R.drawable.s_item027,
    };
    
    private final static int[] S_LABEL_IDS = new int[]{
        R.drawable.s_label009,
        R.drawable.s_label001,
        R.drawable.s_label002,
        R.drawable.s_label003,
        R.drawable.s_label004,
        R.drawable.s_label005,
        R.drawable.s_label006,
        R.drawable.s_label007,
        R.drawable.s_label008,
    };
    
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.stamp_layout);
        
        initUI();
        
    }
    
    private void initUI()
    {
        flip = (ViewFlipper)findViewById(R.id.item_flip);
        
    
        
        flip.addView(generateGrid(S_AC_IDS, AppConstants.ID_AC));
        
        flip.addView(generateGrid(S_HAT_IDS, AppConstants.ID_HAT));
        
        flip.addView(generateGrid(S_ITEM_IDS, AppConstants.ID_ITEM));
        
        flip.addView(generateGrid(S_LABEL_IDS, AppConstants.ID_LABEL));
        
        findViewById(R.id.stamp_left).setOnClickListener(this);
        findViewById(R.id.stamp_left).setOnTouchListener(this);
        
        findViewById(R.id.stamp_right).setOnClickListener(this);
        findViewById(R.id.stamp_right).setOnTouchListener(this);
    }
    
    private GridView generateGrid(int ids[], int id)
    {
        GridView grid = new GridView(this);
        grid.setId(id);
        grid.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        grid.setNumColumns(3);
        grid.setAdapter(new ImageAdapter(this, ids));
        grid.setColumnWidth((int)(90 * getResources().getDisplayMetrics().density));
        grid.setPadding((int)(5 * getResources().getDisplayMetrics().density), 0, 0, 0);
        grid.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id)
            {
                // TODO Auto-generated method stub
                Intent intent = new Intent(StampActivity.this, EditActivity.class);
                intent.setAction(AppConstants.ACTION_SELECT_STAMP);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(AppConstants.KEY_SELECTED_CATEGORY, parent.getId());
                intent.putExtra(AppConstants.KEY_SELECTED_STAMP, position);
                startActivity(intent);
            }
        });
        
        return grid;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch(v.getId())
        {
            case R.id.stamp_left:
            {
                changeAlpha(v, event.getAction());
                break;
            }
            case R.id.stamp_right:
            {
                changeAlpha(v, event.getAction());
                break;
            }
        }
        
        return super.onTouch(v, event);
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.stamp_left:
            {
                flip.setInAnimation(AnimationUtils.loadAnimation(this,  
                    R.anim.push_right_in));  
                flip.setOutAnimation(AnimationUtils.loadAnimation(this,  
                    R.anim.push_right_out));
                flip.showPrevious();
                ((ImageView) findViewById(R.id.stamp_title))
                        .setImageResource(TEXT_IDS[flip.getDisplayedChild()]);
                break;
            }
            case R.id.stamp_right:
            {
                flip.setInAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.push_left_in));
                flip.setOutAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.push_left_out));
                flip.showNext();
                ((ImageView) findViewById(R.id.stamp_title))
                        .setImageResource(TEXT_IDS[flip.getDisplayedChild()]);
                break;
            }
        }
        super.onClick(v);
    }
}
